import { VerticalBox } from "std-widgets.slint";

export component MainWindow inherits Window {
    preferred-width: 800px;
    preferred-height: 450px;

    title: title_text;
    icon: @image-url("res/logo.svg");
    background: #262626;

    in property <image> used_image;
    out property <bool> popup_is_success;
    out property <string> popup_text;
    in property <string> title_text: "scissors";
    in property <string> color_mode: "RGB"; // or "RGBA"
    out property <length> current_width;
    out property <length> current_height;

    callback open_file();
    callback save_as();
    callback about();
    callback blue_to_white();
    callback set_as_one_inch();
    callback show_popup(bool, string);

    show_popup(b, s) => {
        popup_is_success = b;
        popup_text = s;
        popup.show();
        popuptimer.start();
    }

    // 消息提示
    popup := PopupWindow {
        y: 4px;
        width: parent.width;

        Rectangle {
            background: popup_is_success ? #00b300 : #b32400;
            width: parent.width / 3;
            border-radius: 2px;

            VerticalBox {
                padding: 4px;

                Text {
                    horizontal-alignment: center;
                    wrap: word-wrap;
                    text: popup_text;
                    color: white;
                }
            }
        }
    }
    
    // 定时关闭 popup
    popuptimer := Timer {
        interval: 3s;
        running: false;
        triggered => {
            popup.close();
            self.stop();
        }
    }

    // 菜单栏
    MenuBar {
        Menu {
            title: "文件(F)";
            MenuItem {
                title: "打开文件";
                activated => open_file();
            }
            MenuItem {
                enabled: used_image.width > 0;
                title: "另存为...";
                activated => save_as();
            }
        }
        Menu {
            title: "编辑(E)";
            MenuItem {
                title: "蓝底转白底";
                enabled: used_image.width > 0;
                activated => blue_to_white();
            }
            MenuItem {
                title: "调整为1寸照";
                enabled: used_image.width > 0;
                activated => set_as_one_inch();
            }
        }
        Menu {
            title: "帮助(H)";
            MenuItem {
                title: "关于";
                activated => about();
            }
        }
    }

    // 主界面
    VerticalBox {
        width: parent.width;
        height: parent.height;

        Rectangle {
            img := Image {
                source: used_image;
                width: used_image.width * 1px < parent.width ? used_image.width * 1px : parent.width;
                height: used_image.height * 1px < parent.height ? used_image.height * 1px : parent.height;
                image-fit: contain;
            }
            TouchArea {
                enabled: used_image.width > 0;
                scroll-event(event) => {
                    let step = 50px;
                    root.current_width = img.width;
                    root.current_height = img.height;

                    if (event.delta_y > 0) {
                        root.current_width += step;
                        root.current_height += step;
                    } else {
                        root.current_width -= step;
                        root.current_height -= step;
                    }

                    img.width = root.current_width < 100px ? 100px : root.current_width;
                    img.height = root.current_height < 100px ? 100px : root.current_height;

                    accept;
                }
            }
        }
    }
}


export component AboutBox inherits Window {
  width: 300px;
  height: 200px;
  title: "关于scissors";

  Rectangle {
    Text {
      text: "版本：0.1.0\n日期：2025-09-16\n作者：paper tiger\n邮箱：ljxh2024@qq.com";
    }
  }
}